package com.bestcem.xm.member.controller.v2;

import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.controller.v2.param.GetImportProgressQuery;
import com.bestcem.xm.member.controller.v2.param.HttpGetImportRecordParam;
import com.bestcem.xm.member.service.ImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 导入记录相关controller
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/8/5
 */
@Slf4j
@RestController
@RequestMapping("member/v2/import")
public class ImportController {

    /**
     * 导入记录service
     */
    @Resource
    private ImportService importService;

    /**
     * 检查当前联系人是否有正在导入的状态
     *
     * @param mType
     * @return
     */
    @GetMapping("/check")
    public XmResultJson checkMemberImport(@RequestParam("mtype") Integer mType,
                                          HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        return importService.checkImport(mType, orgId);
    }

    /**
     * 获取个人联系人导入记录
     *
     * @param query
     * @return
     */
    @GetMapping("/ind/record")
    public XmResultJson getIndImportRecord(HttpGetImportRecordParam query,
                                               HttpServletRequest request) {
        // 获取 orgId
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        query.setOrgId(orgId);
        query.setUserId(userId);
        return importService.listImportRecords(query);
    }

    /**
     * 获取机构联系人导入记录
     *
     * @param query
     * @return
     */
    @GetMapping("/ins/record")
    public XmResultJson getInsImportRecord(HttpGetImportRecordParam query,
                                               HttpServletRequest request) {
        // 获取 orgId
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        query.setOrgId(orgId);
        query.setUserId(userId);
        return importService.listImportRecords(query);
    }

    /**
     * 获取内部联系人导入记录
     *
     * @param query
     * @return
     */
    @GetMapping("/int/record")
    public XmResultJson getIntImportRecord(HttpGetImportRecordParam query,
                                               HttpServletRequest request) {
        // 获取 orgId
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        query.setOrgId(orgId);
        query.setUserId(userId);
        return importService.listImportRecords(query);
    }

    /**
     * 获取导入进度（前端在导入的时候会轮询该接口）
     *
     * @param query
     * @return
     */
    @PostMapping("/progress")
    public XmResultJson getProgress(@Valid @RequestBody GetImportProgressQuery query,
                                        HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        query.setOrgId(orgId);
        return importService.getImportProgress(query);
    }

    /**
     * 获取联系人导入失败详情
     *
     * @param recordId
     * @param page
     * @param rowsPerPage
     * @return
     */
    @GetMapping("/record/detail")
    public XmResultJson getRecordDetail(@RequestParam("record_id") String recordId,
                                            @RequestParam("page") Integer page,
                                            @RequestParam("rowsPerPage") Integer rowsPerPage,
                                            HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        return importService.listImportDetails(recordId, orgId, page, rowsPerPage);
    }


    /**
     * 根据id获取导入记录详情（在导入完成之后会调用该接口）
     *
     * @param id
     * @return
     */
    @GetMapping("/record/{id}")
    public XmResultJson getImportRecord(@PathVariable String id,
                                            HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        return importService.getImportRecord(orgId, id);
    }
}
